package com.stgl.action;


import com.alibaba.fastjson.JSON;
import com.stgl.fileimage.ImgEditor;
import com.stgl.service.LoginService;
import com.stgl.util.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/")
public class login {
    @Resource
    private LoginService loginService;

    @RequestMapping("onlogin.html")
    @ResponseBody
    public Map<String,Object>onlogin(HttpServletRequest request, HttpServletResponse response){
        String uname=request.getParameter("uname").trim();
        String upwd=request.getParameter("upwd").trim();
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("uname",uname);
        gmap.put("upwd", MD5.MD5(upwd));
        Map<String,Object>rmap=loginService.onlogin(gmap);
        if(rmap.get("result").toString().equals("true")){
            HttpSession session=((HttpServletRequest)request).getSession();
            if(rmap.get("USID")!=null){
                session.setAttribute("usid",rmap.get("USID").toString());
            }
            if(rmap.get("USNAME")!=null){
                session.setAttribute("usname",rmap.get("USNAME").toString());
            }
            if(rmap.get("USSTATE")!=null){
                session.setAttribute("usstate",rmap.get("USSTATE").toString());
            }
            if(rmap.get("CSID")!=null){
                session.setAttribute("csid",rmap.get("CSID").toString());
            }
            if(rmap.get("PCAID")!=null){
                session.setAttribute("pcaid",rmap.get("PCAID").toString());
            }
            if(rmap.get("PCALV")!=null){
                session.setAttribute("pcalv",rmap.get("PCALV").toString());
            }

            if(rmap.get("CSNAME")!=null){
                session.setAttribute("csname",rmap.get("CSNAME").toString());
            }

            if(rmap.get("SSDQ")!=null){
                session.setAttribute("ssdq",rmap.get("SSDQ").toString());
            }

            rmap.put("jsessionid",session.getId());
        }
        return rmap;
    }



    @RequestMapping("userInfo.html")
    @ResponseBody
    public Map<String,Object>userInfo(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object>gmap=new HashMap<String,Object>();
        HttpSession session=((HttpServletRequest)request).getSession();
        if(session.getAttribute("usid")!=null){
            gmap.put("USID",session.getAttribute("usid").toString());
            gmap.put("login",true);
        }else{
            gmap.put("login",false);
        }

        if(session.getAttribute("usname")!=null){
            gmap.put("USNAME",session.getAttribute("usname").toString());
        }

        if(session.getAttribute("usstate")!=null){
            gmap.put("USSTATE",session.getAttribute("usstate").toString());
        }

        if(session.getAttribute("csid")!=null){
            gmap.put("CSID",session.getAttribute("csid").toString());
        }

        if(session.getAttribute("pcaid")!=null){
            gmap.put("PCAID",session.getAttribute("pcaid").toString());
        }

        if(session.getAttribute("pcalv")!=null){
            gmap.put("PCALV",session.getAttribute("pcalv").toString());
        }

        if(session.getAttribute("csname")!=null){
            gmap.put("CSNAME",session.getAttribute("csname").toString());
        }
        if(session.getAttribute("ssdq")!=null){
            gmap.put("SSDQ",session.getAttribute("ssdq").toString());
        }

        if(session.getAttribute("jsessionid")!=null){
            gmap.put("jsessionid",session.getAttribute("jsessionid").toString());
        }
        return gmap;
    }



    @RequestMapping("getArea.html")
    @ResponseBody
    public List<Map<String,Object>>getArea(HttpServletRequest request, HttpServletResponse response) {
        //查询播州区镇
        List<Map<String,Object>>lisarea=loginService.getArea();
        return lisarea;
    }



    @RequestMapping("getnbz.html")
    @ResponseBody
    public List<Map<String,Object>>getnbz(HttpServletRequest request, HttpServletResponse response) {
        //查询南白镇的社区
        List<Map<String,Object>>lisarea=loginService.getnbz();
        return lisarea;
    }



    @RequestMapping("getAllCampus.html")
    @ResponseBody
    public List<Map<String,Object>>getAllCampus(HttpServletRequest request, HttpServletResponse response) {
        //查询所有校区
        List<Map<String,Object>>campus=loginService.getAllCampus();
        return campus;
    }


    @RequestMapping("/filesUpload.html")
    public @ResponseBody
    List<Object> filesUpload(@RequestParam(value = "file") MultipartFile[] files, HttpServletRequest request, HttpSession session) throws IOException {
        List<Object> fileurllist=new ArrayList<Object>();

        Map<String, Object> fileMap = new HashMap<String, Object>();
        if(files!=null&&files.length>0){
            //循环获取file数组中得文件
            for(int i = 0;i<files.length;i++){
                MultipartFile file = files[i];
                //保存文件
                Map<String, Object> fileurl = new ImgEditor().uploadFilemap(file,request,session);
                fileurllist.add(fileurl);
            }
        }
        return fileurllist;
    }

    @RequestMapping("/downloadfile.html")
    public void downloadfile(HttpServletRequest request, HttpServletResponse resp,HttpSession session) throws IOException {
        String fileurl=request.getParameter("fileurl").trim();
        ExportExcel.downloadfile(resp, request,fileurl);
    }





    @RequestMapping("updateUaccount.html")
    @ResponseBody
    public Map<String,Object> updateUaccount(HttpServletRequest request, HttpServletResponse response){
        String olduname=request.getParameter("olduname").trim();
        String uname=request.getParameter("uname").trim();
        String upwd=request.getParameter("upwd").trim();
        String oupwd=request.getParameter("oupwd").trim();
        Map<String,Object>rmap=new HashMap<>();
        String usid="";
        HttpSession session=((HttpServletRequest)request).getSession();
        if(session.getAttribute("usid")!=null){
            usid=session.getAttribute("usid").toString();
        }else if(request.getParameter("usid")!=null && !request.getParameter("usid").toString().equals("")){
            usid=request.getParameter("usid").trim();
        }else{
            rmap.put("result",false);
            rmap.put("msg","无法获取系统id！");
            return rmap;
        }
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("usid",usid);
        gmap.put("olduname",olduname);
        gmap.put("uname",uname);
        gmap.put("newupwd",MD5.MD5(upwd));
        gmap.put("oupwd",MD5.MD5(oupwd));
        Map<String,Object>getmap=loginService.updateUaccount(gmap);
        if(getmap.get("result").toString().equals("true")){
            session.removeAttribute("usid");
            session.removeAttribute("usname");
            session.removeAttribute("usstate");
            session.removeAttribute("csid");
            session.removeAttribute("pcaid");
            session.removeAttribute("pcalv");
            session.removeAttribute("csname");
            session.removeAttribute("ssdq");
        }
        return getmap;
    }


    @RequestMapping("ZXlogin.html")
    @ResponseBody
    public void ZXlogin(HttpServletRequest request, HttpServletResponse response){
        HttpSession session=((HttpServletRequest)request).getSession();
        session.removeAttribute("usid");
        session.removeAttribute("usname");
        session.removeAttribute("usstate");
        session.removeAttribute("csid");
        session.removeAttribute("pcaid");
        session.removeAttribute("pcalv");
        session.removeAttribute("csname");
        session.removeAttribute("ssdq");
    }



    @RequestMapping("updateUpwd.html")
    @ResponseBody
    public Map<String,Object> updateUpwd(HttpServletRequest request, HttpServletResponse response){
        String usid=request.getParameter("usid");
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("usid",usid);
        gmap.put("uspwd", MD5.MD5("000000"));
        Map<String,Object>rmap=new HashMap<String,Object>();
        int m=loginService.updateUsersA(gmap);
        if(m>0){
            rmap.put("result",true);
            rmap.put("msg","保存成功!");
            return rmap;
        }
        rmap.put("result",false);
        rmap.put("msg","添加失败,请联系管理员");
        return rmap;
    }

}
